package com.yaodian100.ec.activity;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.activity.entity.ActivityPage;
import com.yaodian100.ec.activity.entity.ActivityStyleGroup;
import com.yaodian100.ec.activity.service.ActivityPageGenerateService;
import com.yaodian100.ec.activity.service.ActivityPageService;
import com.yaodian100.ec.activity.service.ActivityStyleGroupService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.common.web.AbstractAction;

public class ActivityPageOutputAction extends AbstractAction implements ServletResponseAware {

	private static final long serialVersionUID = 2239472533981086767L;
	
	@Autowired
	private ActivityStyleGroupService activityStyleGroupService;
	@Autowired
	private ActivityPageService activityPageService;
	@Autowired
	private ActivityPageGenerateService activityPageGenerateService;
	
	private ActivityPage activityPage;
	
	private List<ActivityStyleGroup> styleGroups;
	
	private HttpServletResponse response;
	private String expectedOnlineDate;
	
	private Long genPageId;
	
	@Override
	public void prepare() throws Exception {
		super.prepare();
		
		this.styleGroups = this.activityStyleGroupService.getAll(0, -1, new String[]{"id asc"});
	}
	
	public String editPageOutput() {
		this.activityPage = this.activityPageService.get(this.activityPage.getId());
		if (this.activityPage.getExpectedOnlineDate() != null) {
			this.expectedOnlineDate = DateUtil.convertDateToString(EcConstant.PATTERN_DATE, this.activityPage.getExpectedOnlineDate());
		}
		return "editPageOutput";
	}

	public void preview() throws Exception {
		if (activityPage == null) return;
		ActivityPage previewPage = this.activityPageService.get(this.activityPage.getId());
		previewPage.setStyleId(this.activityPage.getStyleId());
		if (this.expectedOnlineDate != null) {
			previewPage.setExpectedOnlineDate(DateUtil.convertStringToDate(this.expectedOnlineDate));
		}
		OutputStream out = this.response.getOutputStream();
		this.activityPageGenerateService.preview(previewPage, out);
	}
	
	public String generate() {
		if (activityPage == null && this.genPageId == null) return null;
		ActivityPage page;
		if (this.genPageId != null) {
			page = this.activityPageService.get(this.genPageId);
		} else {
			page = this.activityPageService.get(this.activityPage.getId());
			page.setStyleId(this.activityPage.getStyleId());
			if (this.expectedOnlineDate != null) {
				page.setExpectedOnlineDate(DateUtil.convertStringToDate(this.expectedOnlineDate));
			}
			page.setCustomFileName(this.activityPage.getCustomFileName());
			
		}
		
		String filepath = this.activityPageGenerateService.generate(page);
		String pageLocation = ServerValue.getFrontHostname() + filepath;
		page.setPageLocation(pageLocation);
		
		this.activityPage = this.activityPageService.save(page);
		
		return "listPage";
	}
	
	public ActivityStyleGroupService getActivityStyleGroupService() {
		return activityStyleGroupService;
	}

	public void setActivityStyleGroupService(
			ActivityStyleGroupService activityStyleGroupService) {
		this.activityStyleGroupService = activityStyleGroupService;
	}

	public List<ActivityStyleGroup> getStyleGroups() {
		return styleGroups;
	}

	public void setStyleGroups(List<ActivityStyleGroup> styleGroups) {
		this.styleGroups = styleGroups;
	}

	public ActivityPage getActivityPage() {
		return activityPage;
	}

	public void setActivityPage(ActivityPage activityPage) {
		this.activityPage = activityPage;
	}

	public ActivityPageService getActivityPageService() {
		return activityPageService;
	}

	public void setActivityPageService(ActivityPageService activityPageService) {
		this.activityPageService = activityPageService;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public ActivityPageGenerateService getActivityPageGenerateService() {
		return activityPageGenerateService;
	}

	public void setActivityPageGenerateService(
			ActivityPageGenerateService activityPageGenerateService) {
		this.activityPageGenerateService = activityPageGenerateService;
	}

	public String getExpectedOnlineDate() {
		return expectedOnlineDate;
	}

	public void setExpectedOnlineDate(String expectedOnlineDate) {
		this.expectedOnlineDate = expectedOnlineDate;
	}

	public Long getGenPageId() {
		return genPageId;
	}

	public void setGenPageId(Long genPageId) {
		this.genPageId = genPageId;
	}


	
	

}
